//
//  MOPersonInfoViewController.m
//  MobileOffice
//
//  Created by 晓 on 2019/6/7.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOPersonInfoViewController.h"
#import "MOPersonInfoCell.h"

@interface MOPersonInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dateSource;

@end

@implementation MOPersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    [self initData];
    
    [self requestData];
}

- (void)initView{
    self.title = @"个人信息";
    [self.view addSubview:self.tableView];
}

- (void)initData{
    
    NSArray *titleArr = @[@"头像",@"姓名",@"电子邮箱",@"联系电话",@"部门",@"个人描述"];
    NSArray *contentArr = @[@"",@"",@"",@"",@"",@""];
    self.dateSource = [NSMutableArray array];
    for (int i = 0; i <titleArr.count; i++) {
        MOPersonInfoModel *model = [[MOPersonInfoModel alloc] init];
        model.titleStr = titleArr[i];
        model.contentStr = contentArr[i];
        [self.dateSource addObject:model];
    }
    [self.tableView reloadData];
}

-(void)requestData
{
    NSString *postUrl = [NSString stringWithFormat:@"%@?userId=%@&authorization=%@",USER_GETBYID_URL,[MOUser sharedMOUser].userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"获取用户信息:%@",responseObject);
        if ([responseObject[@"code"] integerValue]==0) { //0：接口正常  1：接口异常
            
            if ([responseObject[@"status"] integerValue]==0) {
                for (int i = 0; i<self.dateSource.count; i++) {
                    MOPersonInfoModel *model = self.dateSource[i];
                    switch (i) {
                        case 1:
                            model.contentStr = [NSString passNullForString:responseObject[@"name"]];
                            break;
                        case 2:
                            model.contentStr = [NSString passNullForString:responseObject[@"email"]];
                            break;
                        case 3:
                            model.contentStr = [NSString passNullForString:responseObject[@"phone"]];
                            break;
                        case 4:
                            model.contentStr = [NSString passNullForString:responseObject[@"org_name"]];
                            break;
                        case 5:
                            model.contentStr = [NSString passNullForString:responseObject[@"bz"]];
                            break;
                            
                        default:
                            break;
                    }
                }
                [self.tableView reloadData];
            }else{
                [[ADShowMessage shareShowMessage] showTextOnly:@"用户锁定" showInView:self.view];
            }
            
        }else{
            if ([responseObject[@"msg"] isKindOfClass:[NSString class]]) {
                [[ADShowMessage shareShowMessage] showTextOnly:[NSString passNullForString:responseObject[@"msg"]] showInView:self.view];
            }else{
                [[ADShowMessage shareShowMessage] showTextOnly:@"接口异常" showInView:self.view];
            }
        }
    } failure:^(NSError *error) {
        
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dateSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOPersonInfoCell *personInfoCell = [MOPersonInfoCell cellWithTableView:tableView];
    personInfoCell.model = self.dateSource[indexPath.section];
    if (indexPath.section == 0) {
        personInfoCell.userImageView.hidden = NO;
        personInfoCell.contentLabel.hidden = YES;
    }
    else{
        personInfoCell.userImageView.hidden = YES;
        personInfoCell.contentLabel.hidden = NO;
    }
    return personInfoCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return kHConversions(73);
    }
    else{
        return kHConversions(54);
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


#pragma mark - 懒加载
- (UITableView *)tableView{
    if (_tableView ==nil ) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KBackGrayColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return  _tableView;
}

@end
